package cn.bdqn.smbms.controller;

import cn.bdqn.smbms.entity.Provider;
import cn.bdqn.smbms.entity.User;
import cn.bdqn.smbms.service.ProviderService;
import cn.bdqn.smbms.util.PageBean;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;


    @RequestMapping("provider.html")
    public String showProvider(
            @RequestParam(value = "pageNo", required = false, defaultValue = "1")
                    int pageNo,
            @RequestParam(value = "queryProCode", required = false)
                    String queryProCode,
            @RequestParam(value = "queryProName", required = false)
                    String queryProName,
            Model model
    ) {
        int pageSize = 3;
        PageBean<Provider> pageBean = providerService.findByPage(queryProCode, queryProName, pageNo, pageSize);
        model.addAttribute("pageBean", pageBean);
        if (queryProCode != null) {
            model.addAttribute("queryProCode", queryProCode);
        }
        if (queryProName != null) {
            model.addAttribute("queryProName", queryProName);
        }
        return "providerlist";
    }

    //显示添加供应商
    @RequestMapping("/addPro")
    public String addPro() {
        return "provideradd";
    }

    //新增
    @RequestMapping("/proAdd")
    public String providerAdd(
            Provider provider,
            HttpSession session,
            MultipartFile proCompany) {
        String path = session.getServletContext().getRealPath("statics"+ File.separator +"upload");
        User user = (User) session.getAttribute("userSession");
        provider.setCreatedBy(user.getId());
        if(!proCompany.isEmpty()){
            File file = new File(path,proCompany.getOriginalFilename());
            try {
                proCompany.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        provider.setProCompany(proCompany.getOriginalFilename());
        int ret = providerService.addProvider(provider);
        if (ret > 0) {
            return "success1";
        } else {
            return "error1";
        }
    }

    //删除
    @ResponseBody
    @RequestMapping("/delProvider/{id}")
    public String delPro(
            @PathVariable()
                    Integer id
    ) {
        Map<String, Object> map = new HashMap<>();
        Provider provider = providerService.findById(id);
        if (provider == null) {
            map.put("delResult", "notexist");
        } else {
            int ret = providerService.delProvider(id);
            if (ret > 0) {
                map.put("delResult", "true");
            } else {
                map.put("delResult", "false");
            }
        }
        return JSON.toJSONString(map);
    }


    @RequestMapping("/modify/{id}")
    public String showID(
            @PathVariable("id")
            Integer id,
            Model model
    ){
        Provider provider = providerService.findById(id);
        model.addAttribute("provider",provider);
        return "providermodify";
    }

    @RequestMapping("/modifyPro")
    public String modifyPro(
            Provider provider,
            HttpSession session
    ){
        User user = (User)session.getAttribute("userSession");
        provider.setModifyBy(user.getId());
        int ret = providerService.updateProvider(provider);
        if(ret>0){
            return "success1";
        }else{
            return "error1";
        }
    }

    //ajax显示详情
    @ResponseBody
    @RequestMapping("/viewProvider/{id}")
    public Provider view(
            @PathVariable("id")
            Integer id
    ){
        Provider provider=providerService.findById(id);
        return provider;
    }

    //ajax验证供应商编码是否存在
    @ResponseBody
    @RequestMapping("/checkProCode")
    public boolean checkPrpCode(
            @RequestParam
            String proCode
    ){
        return providerService.checkProCode(proCode);
    }
}
